﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.Temp
{
	internal class RevitDuctSystemUpdate
	{
		public string ErrorString { get; set; }

		public RevitDuctSystemUpdate(HYDuctSystemDocument doc, Document rvtDoc)
		{
			this.m_Document = doc;
			this.m_RevitDocument = rvtDoc;
		}

		public bool CheckDuckFitting(ref string familyName)
		{
			IEnumerable<HYMEPObject> enumerable = from ft in this.m_Document.DuctSystem.Objects
			where ft is HYDuctFitting
			select ft;
			if (enumerable == null || enumerable.Count<HYMEPObject>() < 1)
			{
				return true;
			}
			List<string> list = new List<string>();
			foreach (HYMEPObject hymepobject in enumerable)
			{
				ElementId elementId = new ElementId(((HYDuctFitting)hymepobject).Id);
				Element elementById = this.m_RevitDocument.GetElementById(elementId);
				if (elementById != null)
				{
					FamilyInstance familyInstance = elementById as FamilyInstance;
					if (familyInstance != null)
					{
						string name = familyInstance.Symbol.Family.Name;
						if (!list.Contains(name))
						{
							list.Add(name);
							MechanicalFitting mechanicalFitting = familyInstance.MEPModel as MechanicalFitting;
                            if (mechanicalFitting != null && ((int)mechanicalFitting.PartType == 6 || (int)mechanicalFitting.PartType == 8) && !this.IsDuctFittingAdaptive(familyInstance))
							{
								familyName = name;
								return false;
							}
						}
					}
				}
			}
			return true;
		}

		public bool IsDuctFittingAdaptive(FamilyInstance fitting)
		{
			Document document = fitting.Document;
			if (document.IsLinked || document.IsReadOnly)
			{
				return false;
			}
			Transaction transaction = null;
			try
			{
				MechanicalFitting mechanicalFitting = fitting.MEPModel as MechanicalFitting;
				transaction = new Transaction(this.m_RevitDocument, "FittingAdaptive");
				transaction.Start();
				double num = 0.0;
				double num2 = 0.0;
				foreach (object obj in mechanicalFitting.ConnectorManager.Connectors)
				{
					Connector connector = (Connector)obj;
					if (num < connector.Width)
					{
						num = connector.Width;
					}
					if (num2 < connector.Height)
					{
						num2 = connector.Height;
					}
				}
				foreach (object obj2 in mechanicalFitting.ConnectorManager.Connectors)
				{
					Connector connector2 = (Connector)obj2;
					SubTransaction subTransaction = new SubTransaction(this.m_RevitDocument);
					subTransaction.Start();
					num += 0.16404199475065617;
					num2 += 0.16404199475065617;
					connector2.Width = num;
					connector2.Height = num2;
					subTransaction.Commit();
					this.m_RevitDocument.Regenerate();
					foreach (object obj3 in mechanicalFitting.ConnectorManager.Connectors)
					{
						Connector connector3 = (Connector)obj3;
						if (!connector2.Origin.IsAlmostEqualTo(connector3.Origin) && (Math.Abs(connector2.Width - connector3.Width) < 0.032808398950131233 || Math.Abs(connector2.Height - connector3.Height) < 0.032808398950131233))
						{
							return false;
						}
					}
				}
				transaction.RollBack();
			}
			catch (Exception)
			{
				return false;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() != 2)
				{
					transaction.RollBack();
				}
			}
			return true;
		}

		public bool Update()
		{
			bool flag = true;
			IOrderedEnumerable<HYMEPObject> orderedEnumerable = from strt in this.m_Document.DuctSystem.Objects
			where strt is HYDuctStraight
			orderby strt.Flow
			select strt;
			IEnumerable<HYMEPObject> enumerable = from ft in this.m_Document.DuctSystem.Objects
			where ft is HYDuctFitting
			select ft;
			IEnumerable<HYMEPObject> enumerable2 = from acc in this.m_Document.DuctSystem.Objects
			where acc is HYDuctAccessory
			select acc;
			this.m_RevitDucts = new List<Duct>();
			bool result;
			using (Transaction transaction = new Transaction(this.m_RevitDocument, "盈建科水力计算结果赋回图面"))
			{
				try
				{
					transaction.Start();
					foreach (HYMEPObject hymepobject in orderedEnumerable)
					{
						HYDuctStraight hyductStraight = (HYDuctStraight)hymepobject;
						ElementId elementId = new ElementId(hyductStraight.Id);
						Element elementById = this.m_RevitDocument.GetElementById(elementId);
						if (elementById != null)
						{
							Duct duct = elementById as Duct;
							if (duct != null)
							{
								this.m_RevitDucts.Add(duct);
								flag &= RevitDuctSystemUpdate.ToDuct(hyductStraight, duct);
							}
						}
					}
					foreach (HYMEPObject hymepobject2 in enumerable2)
					{
						HYDuctAccessory hyductAccessory = (HYDuctAccessory)hymepobject2;
						ElementId elementId2 = new ElementId(hyductAccessory.Id);
						Element elementById2 = this.m_RevitDocument.GetElementById(elementId2);
						if (elementById2 != null)
						{
							FamilyInstance familyInstance = elementById2 as FamilyInstance;
							if (familyInstance != null)
							{
								flag &= RevitDuctSystemUpdate.ToAccessory(hyductAccessory, familyInstance);
							}
						}
					}
					foreach (HYMEPObject hymepobject3 in enumerable)
					{
						HYDuctFitting hyductFitting = (HYDuctFitting)hymepobject3;
						ElementId elementId3 = new ElementId(hyductFitting.Id);
						Element elementById3 = this.m_RevitDocument.GetElementById(elementId3);
						if (elementById3 != null)
						{
							FamilyInstance familyInstance2 = elementById3 as FamilyInstance;
							if (familyInstance2 != null)
							{
								flag &= RevitDuctSystemUpdate.ToFitting(hyductFitting, familyInstance2);
							}
						}
					}
					this.m_RevitDocument.Regenerate();
					transaction.Commit();
					transaction.Start();
					YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
					foreach (HYMEPObject hymepobject4 in orderedEnumerable)
					{
						ElementId elementId4 = new ElementId(((HYDuctStraight)hymepobject4).Id);
						Element elementById4 = this.m_RevitDocument.GetElementById(elementId4);
						if (elementById4 != null)
						{
							Duct duct2 = elementById4 as Duct;
							if (duct2 != null)
							{
								hyrotateFamilyInstance.MovePiping(duct2);
							}
						}
					}
					transaction.Commit();
					this.ErrorString = "更新操作完成！";
					flag &= this.SaveLinkedUpdateData(orderedEnumerable.ToList<HYMEPObject>(), enumerable2.ToList<HYMEPObject>(), enumerable.ToList<HYMEPObject>());
					result = true;
				}
				catch (Exception ex)
				{
					this.ErrorString = ex.Message;
					if (transaction != null && (int)transaction.GetStatus() != 3)
					{
						transaction.RollBack();
					}
					result = false;
				}
			}
			return result;
		}

		protected bool SaveLinkedUpdateData(List<HYMEPObject> hyStrts, List<HYMEPObject> Accessories, List<HYMEPObject> Fittings)
		{
			bool flag = true;
			List<LinkedDocument> list = RevitUtility.LinkedDocuments.ToList<LinkedDocument>();
			if (list.Count > 0 && YJKMessageBox.Show("当前文档存在链接文件，是否保存链接文件改动数据？", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
			{
				string text = this.m_RevitDocument.Title;
				string arg = text;
				if (text.LastIndexOf(".rvt") > 0)
				{
					arg = text.Substring(0, text.LastIndexOf(".rvt"));
				}
				string text2 = string.Format("风管水力计算_InFile_{0}.xml", arg);
				string empty = string.Empty;
				SaveFileDialog saveFileDialog = new SaveFileDialog();
				saveFileDialog.Filter = "数据更新文件|*.xml";
				if (!string.IsNullOrEmpty(this.lastSelectedPath))
				{
					saveFileDialog.InitialDirectory = this.lastSelectedPath;
				}
				saveFileDialog.RestoreDirectory = true;
				saveFileDialog.FilterIndex = 1;
				saveFileDialog.FileName = text2;
				if (saveFileDialog.ShowDialog() == DialogResult.OK)
				{
					try
					{
						text2 = saveFileDialog.FileName;
						this.lastSelectedPath = text2;
						List<string> list2 = new List<string>();
						foreach (LinkedDocument linkedDocument in list)
						{
							text = linkedDocument.Document.Title;
							if (text.LastIndexOf(".rvt") > 0)
							{
								text = text.Substring(0, text.LastIndexOf(".rvt"));
							}
							list2.Add(text);
						}
						flag &= HYUpdateLinkedDocXML.AddRootAndFileListNodes(text2, MajorSystemType.DuctOfHVACSystem, list2);
						foreach (LinkedDocument linkedDocument2 in list)
						{
							text = linkedDocument2.Document.Title;
							if (text.LastIndexOf(".rvt") > 0)
							{
								text = text.Substring(0, text.LastIndexOf(".rvt"));
							}
							List<HYUpdateElementInfo> list3 = new List<HYUpdateElementInfo>();
							this.UpdateLinkedPipes(linkedDocument2.Document, text, list3, hyStrts);
							this.UdpateLinkedAccessories(linkedDocument2.Document, text, list3, Accessories);
							this.UpdateLinkedFittings(linkedDocument2.Document, text, list3, Fittings);
							flag &= HYUpdateLinkedDocXML.AddElementListToXML(text2, text, list3);
						}
					}
					catch (Exception ex)
					{
						ex.ToString();
						flag = false;
						YJKMessageBox.Error("保存链接文件数据失败！");
					}
				}
			}
			return flag;
		}

		protected void UpdateLinkedPipes(Document doc, string docTitle, List<HYUpdateElementInfo> changedElements, List<HYMEPObject> hyStrts)
		{
			foreach (HYMEPObject hymepobject in hyStrts)
			{
				HYDuctStraight hyductStraight = (HYDuctStraight)hymepobject;
				Element elementById = doc.GetElementById(new ElementId(hyductStraight.Id));
				if (elementById != null)
				{
					HYUpdateElementInfo hyupdateElementInfo = new HYUpdateElementInfo();
					hyupdateElementInfo.DocumentTitle = docTitle;
					hyupdateElementInfo.ElementID = hyductStraight.Id;
					Category category = elementById.Document.Settings.Categories.get_Item(BuiltInCategory.OST_DuctCurves);
					hyupdateElementInfo.CategoryName = category.Name;
					hyupdateElementInfo.Diameter = (double)hyductStraight.Diameter;
					hyupdateElementInfo.Width = hyductStraight.Width;
					hyupdateElementInfo.Height = hyductStraight.Height;
					hyupdateElementInfo.FLow = hyductStraight.Flow;
					hyupdateElementInfo.Velocity = hyductStraight.Velocity;
					hyupdateElementInfo.Shape = (HYUpdateElementShape)hyductStraight.Shape;
					changedElements.Add(hyupdateElementInfo);
				}
			}
		}

		protected void UdpateLinkedAccessories(Document doc, string docTitle, List<HYUpdateElementInfo> changedElements, List<HYMEPObject> Accessories)
		{
			foreach (HYMEPObject hymepobject in Accessories)
			{
				HYDuctAccessory hyductAccessory = (HYDuctAccessory)hymepobject;
				Element elementById = doc.GetElementById(new ElementId(hyductAccessory.Id));
				if (elementById != null)
				{
					HYUpdateAttachedEleInfo hyupdateAttachedEleInfo = new HYUpdateAttachedEleInfo();
					hyupdateAttachedEleInfo.DocumentTitle = docTitle;
					hyupdateAttachedEleInfo.ElementID = hyductAccessory.Id;
					Category category = elementById.Document.Settings.Categories.get_Item(BuiltInCategory.OST_DuctAccessory);
					hyupdateAttachedEleInfo.CategoryName = category.Name;
					HYDuctStraight hyductStraight = hyductAccessory.GetHostPipeline() as HYDuctStraight;
					if (hyductStraight != null)
					{
						HYDuctSpecification hyductSpecification = (HYDuctSpecification)hyductStraight.Section;
						if (hyductSpecification != null)
						{
							hyupdateAttachedEleInfo.OutElementInfo = new HYUpdateElementInfo();
							hyupdateAttachedEleInfo.OutElementInfo.DocumentTitle = docTitle;
							this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.OutElementInfo, hyductStraight, hyductSpecification);
						}
					}
					changedElements.Add(hyupdateAttachedEleInfo);
				}
			}
		}

		protected void UpdateLinkedFittings(Document doc, string docTitle, List<HYUpdateElementInfo> changedElements, List<HYMEPObject> Fittings)
		{
			foreach (HYMEPObject hymepobject in Fittings)
			{
				HYDuctFitting hyductFitting = (HYDuctFitting)hymepobject;
				Element elementById = doc.GetElementById(new ElementId(hyductFitting.Id));
				FamilyInstance familyInstance = elementById as FamilyInstance;
				if (elementById != null && familyInstance != null)
				{
					MechanicalFitting mechanicalFitting = familyInstance.MEPModel as MechanicalFitting;
					if (mechanicalFitting != null)
					{
						HYUpdateAttachedEleInfo hyupdateAttachedEleInfo = new HYUpdateAttachedEleInfo();
						hyupdateAttachedEleInfo.DocumentTitle = docTitle;
						hyupdateAttachedEleInfo.ElementID = hyductFitting.Id;
						Category category = elementById.Document.Settings.Categories.get_Item(BuiltInCategory.OST_DuctFitting);
						hyupdateAttachedEleInfo.CategoryName = category.Name;
                        switch ((int)mechanicalFitting.PartType)
						{
						case 5:
						{
							HYDuctElbow hyductElbow = hyductFitting as HYDuctElbow;
							if (hyductElbow != null)
							{
								hyupdateAttachedEleInfo.Horizontal = hyductElbow.IsHorizontal;
								HYDuctStraight hyductStraight = hyductElbow.GetHostPipeline() as HYDuctStraight;
								if (hyductStraight != null)
								{
									HYDuctSpecification hyductSpecification = (HYDuctSpecification)hyductStraight.Section;
									if (hyductSpecification != null)
									{
										hyupdateAttachedEleInfo.OutElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.OutElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.OutElementInfo, hyductStraight, hyductSpecification);
									}
								}
							}
							break;
						}
						case 6:
						{
							HYDuctTee hyductTee = hyductFitting as HYDuctTee;
							if (hyductTee != null)
							{
								HYDuctStraight hyductStraight2 = hyductTee.GetPreviousPipeline() as HYDuctStraight;
								if (hyductStraight2 != null)
								{
									HYDuctSpecification hyductSpecification2 = (HYDuctSpecification)hyductStraight2.Section;
									if (hyductSpecification2 != null)
									{
										hyupdateAttachedEleInfo.InElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.InElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.InElementInfo, hyductStraight2, hyductSpecification2);
									}
								}
								HYDuctStraight hyductStraight3 = hyductTee.GetHostPipeline() as HYDuctStraight;
								if (hyductStraight3 != null)
								{
									HYDuctSpecification hyductSpecification3 = (HYDuctSpecification)hyductStraight3.Section;
									if (hyductSpecification3 != null)
									{
										hyupdateAttachedEleInfo.OutElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.OutElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.OutElementInfo, hyductStraight3, hyductSpecification3);
									}
								}
								HYDuctStraight hyductStraight4 = hyductTee.GetSideHostPipeline() as HYDuctStraight;
								if (hyductStraight4 != null)
								{
									HYDuctSpecification hyductSpecification4 = (HYDuctSpecification)hyductStraight4.Section;
									if (hyductSpecification4 != null)
									{
										hyupdateAttachedEleInfo.SideElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.SideElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.SideElementInfo, hyductStraight4, hyductSpecification4);
									}
								}
							}
							break;
						}
						case 7:
						{
							HYDuctTransition hyductTransition = hyductFitting as HYDuctTransition;
							if (hyductTransition != null)
							{
								HYDuctStraight hyductStraight5 = hyductTransition.GetPreviousPipeline() as HYDuctStraight;
								if (hyductStraight5 != null)
								{
									HYDuctSpecification hyductSpecification5 = (HYDuctSpecification)hyductStraight5.Section;
									if (hyductSpecification5 != null)
									{
										hyupdateAttachedEleInfo.InElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.InElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.InElementInfo, hyductStraight5, hyductSpecification5);
									}
								}
								HYDuctStraight hyductStraight6 = hyductTransition.GetHostPipeline() as HYDuctStraight;
								if (hyductStraight6 != null)
								{
									HYDuctSpecification hyductSpecification6 = (HYDuctSpecification)hyductStraight6.Section;
									if (hyductSpecification6 != null)
									{
										hyupdateAttachedEleInfo.OutElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.OutElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.OutElementInfo, hyductStraight6, hyductSpecification6);
									}
								}
							}
							break;
						}
						case 8:
						{
							HYDuctCross hyductCross = hyductFitting as HYDuctCross;
							if (hyductCross != null)
							{
								HYDuctStraight hyductStraight7 = hyductCross.GetPreviousPipeline() as HYDuctStraight;
								if (hyductStraight7 != null)
								{
									HYDuctSpecification hyductSpecification7 = (HYDuctSpecification)hyductStraight7.Section;
									if (hyductSpecification7 != null)
									{
										hyupdateAttachedEleInfo.InElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.InElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.InElementInfo, hyductStraight7, hyductSpecification7);
									}
								}
								HYDuctStraight hyductStraight8 = hyductCross.GetHostPipeline() as HYDuctStraight;
								if (hyductStraight8 != null)
								{
									HYDuctSpecification hyductSpecification8 = (HYDuctSpecification)hyductStraight8.Section;
									if (hyductSpecification8 != null)
									{
										hyupdateAttachedEleInfo.OutElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.OutElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.OutElementInfo, hyductStraight8, hyductSpecification8);
									}
								}
								HYDuctStraight hyductStraight9 = hyductCross.GetSideHostPipeline() as HYDuctStraight;
								if (hyductStraight9 != null)
								{
									HYDuctSpecification hyductSpecification9 = (HYDuctSpecification)hyductStraight9.Section;
									if (hyductSpecification9 != null)
									{
										hyupdateAttachedEleInfo.SideElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.SideElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.SideElementInfo, hyductStraight9, hyductSpecification9);
									}
								}
								HYDuctStraight hyductStraight10 = hyductCross.GetSide2HostPipeline() as HYDuctStraight;
								if (hyductStraight10 != null)
								{
									HYDuctSpecification hyductSpecification10 = (HYDuctSpecification)hyductStraight10.Section;
									if (hyductSpecification10 != null)
									{
										hyupdateAttachedEleInfo.Side2ElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.Side2ElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.Side2ElementInfo, hyductStraight10, hyductSpecification10);
									}
								}
							}
							break;
						}
						}
						changedElements.Add(hyupdateAttachedEleInfo);
					}
				}
			}
		}

		protected bool AssignAttachElementInfo(Document doc, HYUpdateElementInfo node, HYDuctStraight duct, HYDuctSpecification ds)
		{
			bool result = false;
			if (node != null && duct != null && ds != null)
			{
				node.ElementID = duct.Id;
				node.Diameter = (double)ds.Diameter;
				node.Width = ds.Width;
				node.Height = ds.Height;
				node.Shape = (HYUpdateElementShape)ds.Shape;
				Element elementById = doc.GetElementById(new ElementId(duct.Id));
				if (elementById != null)
				{
					Category category = elementById.Document.Settings.Categories.get_Item(BuiltInCategory.OST_DuctFitting);
					node.CategoryName = category.Name;
				}
				result = true;
			}
			return result;
		}

		private static bool ToDuct(HYDuctStraight hyStraight, Duct element)
		{
			if (hyStraight.Shape == HYShapeProfile.Round)
			{
				RevitUtils.SetDistance(element, BuiltInParameter.RBS_CURVE_DIAMETER_PARAM, (double)hyStraight.Diameter / 1000.0);
			}
			else
			{
				(element.Location as LocationCurve).Curve.GetEndPoint(0);
				(element.Location as LocationCurve).Curve.GetEndPoint(1);
				double value = RevitUtils.GetValue(element, BuiltInParameter.RBS_CURVE_WIDTH_PARAM);
				double value2 = RevitUtils.GetValue(element, BuiltInParameter.RBS_CURVE_HEIGHT_PARAM);
				double num = RevitUtils.DistanceToStd((double)hyStraight.Width / 1000.0);
				double num2 = RevitUtils.DistanceToStd((double)hyStraight.Height / 1000.0);
				if (value2 > value)
				{
					if (num2 > num)
					{
						RevitUtils.SetDistance(element, BuiltInParameter.RBS_CURVE_WIDTH_PARAM, (double)hyStraight.Width / 1000.0);
						RevitUtils.SetDistance(element, BuiltInParameter.RBS_CURVE_HEIGHT_PARAM, (double)hyStraight.Height / 1000.0);
					}
					else
					{
						RevitUtils.SetDistance(element, BuiltInParameter.RBS_CURVE_WIDTH_PARAM, (double)hyStraight.Height / 1000.0);
						RevitUtils.SetDistance(element, BuiltInParameter.RBS_CURVE_HEIGHT_PARAM, (double)hyStraight.Width / 1000.0);
					}
				}
				else if (num2 > num)
				{
					RevitUtils.SetDistance(element, BuiltInParameter.RBS_CURVE_WIDTH_PARAM, (double)hyStraight.Height / 1000.0);
					RevitUtils.SetDistance(element, BuiltInParameter.RBS_CURVE_HEIGHT_PARAM, (double)hyStraight.Width / 1000.0);
				}
				else
				{
					RevitUtils.SetDistance(element, BuiltInParameter.RBS_CURVE_WIDTH_PARAM, (double)hyStraight.Width / 1000.0);
					RevitUtils.SetDistance(element, BuiltInParameter.RBS_CURVE_HEIGHT_PARAM, (double)hyStraight.Height / 1000.0);
				}
			}
			return true;
		}

		private static void ToConnector(HYDuctSpecification ds, Connector conn)
		{
			if (conn == null)
			{
				return;
			}
			try
			{
				if (ds.Shape == HYShapeProfile.Round)
				{
					if (conn.Shape == null)
					{
						conn.Radius = RevitUtils.DistanceToStd((double)ds.Diameter / 2000.0);
					}
                    else if ((int)conn.Shape == 1)
					{
						conn.Width = RevitUtils.DistanceToStd((double)ds.Diameter / 1000.0);
						conn.Height = RevitUtils.DistanceToStd((double)ds.Diameter / 1000.0);
					}
				}
				else
				{
					double num = RevitUtils.DistanceToStd((double)ds.Width / 1000.0);
					double num2 = RevitUtils.DistanceToStd((double)ds.Height / 1000.0);
					double radius = (num2 > num) ? (num2 / 2.0) : (num / 2.0);
					if (conn.Shape == null)
					{
						conn.Radius = radius;
					}
                    else if ((int)conn.Shape == 1)
					{
						conn.Height = num2;
						conn.Width = num;
					}
				}
			}
			catch (Exception)
			{
			}
		}

		private static void ToConnectorVertical(HYDuctSpecification ds, Connector conn)
		{
			if (conn == null)
			{
				return;
			}
			try
			{
				if (ds.Shape == HYShapeProfile.Round)
				{
					conn.Radius = RevitUtils.DistanceToStd((double)ds.Diameter / 2000.0);
				}
				else
				{
					double height = RevitUtils.DistanceToStd((double)ds.Width / 1000.0);
					double width = RevitUtils.DistanceToStd((double)ds.Height / 1000.0);
					conn.Height = height;
					conn.Width = width;
				}
			}
			catch (Exception)
			{
			}
		}

		private static bool ToAccessory(HYDuctAccessory hyAcc, FamilyInstance rvtFi)
		{
			HYDuctStraight hyductStraight = hyAcc.GetHostPipeline() as HYDuctStraight;
			if (hyductStraight == null)
			{
				return false;
			}
			HYDuctSpecification ds = (HYDuctSpecification)hyductStraight.Section;
			foreach (Connector connector in RevitUtils.GetElementConnectors(rvtFi))
			{
				if (connector.Domain == (Autodesk.Revit.DB.Domain)1)
				{
					RevitDuctSystemUpdate.ToConnector(ds, connector);
				}
			}
			return true;
		}

		private static bool ToFitting(HYDuctFitting hyFitting, FamilyInstance rvtFi)
		{
			MechanicalFitting mechanicalFitting = rvtFi.MEPModel as MechanicalFitting;
			if (mechanicalFitting == null)
			{
				return false;
			}
            switch ((int)mechanicalFitting.PartType)
			{
			case 5:
				RevitDuctSystemUpdate.ToElbow(hyFitting as HYDuctElbow, rvtFi);
				break;
			case 6:
				RevitDuctSystemUpdate.ToTee(hyFitting as HYDuctTee, rvtFi);
				break;
			case 7:
				RevitDuctSystemUpdate.ToTrans(hyFitting as HYDuctTransition, rvtFi);
				break;
			case 8:
				RevitDuctSystemUpdate.ToCross(hyFitting as HYDuctCross, rvtFi);
				break;
			case 10:
				RevitDuctSystemUpdate.ToTaper(hyFitting as HYDuctTaper, rvtFi);
				break;
			case 11:
				RevitDuctSystemUpdate.ToTaper(hyFitting as HYDuctTaper, rvtFi);
				break;
			}
			return true;
		}

		private static bool ToTrans(HYDuctTransition hyTrans, FamilyInstance rvtFi)
		{
			HYDuctSpecification hyductSpecification = hyTrans.InPort as HYDuctSpecification;
			HYDuctStraight hyductStraight = hyTrans.GetPreviousPipeline() as HYDuctStraight;
			if (hyductStraight != null)
			{
				RevitDuctSystemUpdate.ToConnector((HYDuctSpecification)hyductStraight.Section, hyductSpecification.Tag as Connector);
			}
			HYDuctSpecification hyductSpecification2 = hyTrans.OutPort as HYDuctSpecification;
			HYDuctStraight hyductStraight2 = hyTrans.GetHostPipeline() as HYDuctStraight;
			if (hyductStraight2 != null)
			{
				RevitDuctSystemUpdate.ToConnector((HYDuctSpecification)hyductStraight2.Section, hyductSpecification2.Tag as Connector);
			}
			return true;
		}

		private static bool ToElbow(HYDuctElbow hyElbow, FamilyInstance rvtFi)
		{
			HYDuctStraight hyductStraight = hyElbow.GetHostPipeline() as HYDuctStraight;
			if (hyductStraight == null)
			{
				return false;
			}
			HYDuctSpecification ds = (HYDuctSpecification)hyductStraight.Section;
			List<Connector> elementConnectors = RevitUtils.GetElementConnectors(rvtFi);
			if (hyElbow.IsHorizontal)
			{
				using (List<Connector>.Enumerator enumerator = elementConnectors.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Connector conn = enumerator.Current;
						RevitDuctSystemUpdate.ToConnector(ds, conn);
					}
					return true;
				}
			}
			foreach (Connector conn2 in elementConnectors)
			{
				RevitDuctSystemUpdate.ToConnectorVertical(ds, conn2);
			}
			return true;
		}

		private static bool ToTee(HYDuctTee hyTee, FamilyInstance rvtFi)
		{
			try
			{
				HYDuctSpecification hyductSpecification = hyTee.InPort as HYDuctSpecification;
				HYPipeline previousPipeline = hyTee.GetPreviousPipeline();
				HYDuctStraight hyductStraight = null;
				if (previousPipeline != null)
				{
					hyductStraight = (previousPipeline as HYDuctStraight);
				}
				if (hyductStraight != null)
				{
					RevitDuctSystemUpdate.ToConnector((HYDuctSpecification)hyductStraight.Section, hyductSpecification.Tag as Connector);
				}
				HYDuctSpecification hyductSpecification2 = hyTee.OutPort as HYDuctSpecification;
				HYDuctStraight hyductStraight2 = null;
				previousPipeline = hyTee.GetPreviousPipeline();
				if (previousPipeline != null)
				{
					hyductStraight2 = (previousPipeline as HYDuctStraight);
				}
				if (hyductStraight2 == null)
				{
					hyductStraight2 = hyductStraight;
				}
				if (hyductStraight2 != null)
				{
					RevitDuctSystemUpdate.ToConnector((HYDuctSpecification)hyductStraight2.Section, hyductSpecification2.Tag as Connector);
				}
				HYDuctSpecification sidePort = hyTee.SidePort;
				HYDuctStraight hyductStraight3 = null;
				previousPipeline = hyTee.GetPreviousPipeline();
				if (previousPipeline != null)
				{
					hyductStraight3 = (previousPipeline as HYDuctStraight);
				}
				if (hyductStraight3 == null)
				{
					hyductStraight3 = hyductStraight;
				}
				if (hyductStraight3 != null)
				{
					RevitDuctSystemUpdate.ToConnector((HYDuctSpecification)hyductStraight3.Section, sidePort.Tag as Connector);
				}
			}
			catch (Exception)
			{
				throw;
			}
			return true;
		}

		private static bool ToCross(HYDuctCross hyCross, FamilyInstance rvtFi)
		{
			RevitDuctSystemUpdate.ToTee(hyCross, rvtFi);
			HYDuctSpecification side2Port = hyCross.Side2Port;
			HYDuctStraight hyductStraight = null;
			HYPipeline side2HostPipeline = hyCross.GetSide2HostPipeline();
			if (side2HostPipeline != null)
			{
				hyductStraight = (side2HostPipeline as HYDuctStraight);
			}
			if (hyductStraight == null)
			{
				hyductStraight = (hyCross.GetPreviousPipeline() as HYDuctStraight);
			}
			if (hyductStraight != null)
			{
				RevitDuctSystemUpdate.ToConnector((HYDuctSpecification)hyductStraight.Section, side2Port.Tag as Connector);
			}
			return true;
		}

		private static bool ToTaper(HYDuctTaper hyTaper, FamilyInstance rvtFi)
		{
			return false;
		}

		private HYDuctSystemDocument m_Document;

		private Document m_RevitDocument;

		private List<Duct> m_RevitDucts;

		protected string lastSelectedPath = string.Empty;
	}
}
